#include <vector>
using namespace std;
struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode()
      : val(0), left(nullptr), right(nullptr){} * TreeNode(int x)
      : val(x),
        left(nullptr), right(nullptr) {}
  TreeNode(int x, TreeNode *left, TreeNode *right)
      : val(x), left(left), right(right) {}
};

void dfs(vector<vector<int>> &res, TreeNode *root, int layer) {
  if (root == nullptr) {
    return;
  }
  if (res.size() <= layer) {
    res.push_back({});
  }
  res[layer].push_back(root->val);
  if (root->left != nullptr)
    dfs(res, root->left, layer + 1);
  if (root->right != nullptr)
    dfs(res, root->right, layer + 1);
}

vector<vector<int>> levelOrder(TreeNode *root) {
  vector<vector<int>> res;
  dfs(res, root, 0);
  return res;
}